﻿using System;

namespace BuzzX8.Xmpp.StreamElements.Builders
{
    public abstract class ErrorElementBuilder<T> : StreamElementBuilder<T> where T : ErrorElement
    {
        protected override void AddAttributeToRootElement(string name, string value)
        { }

        protected override void AddAttributeToCurrentNode(string name, string value)
        {
            if (IsDescriptionLang(name)) RootElement.DescriptionLang = value;
            if (IsErrorName(name)) RootElement.ErrorName = CurrentNodeName;
        }

        private bool IsDescriptionLang(string attributeName)
        {
            return CurrentNodeName == "text" && attributeName == "xml:lang";
        }

        private bool IsErrorName(string attributeName)
        {
            return CurrentNodeName != "text" && CurrentNodeNamespace == RootElement.ErrorInfoNamespace && attributeName == "xmlns";
        }

        protected override void SetCurrentNodeValue(string value)
        {
            if (IsErrorDescription()) RootElement.ErrorDescription = value;
        }

        private bool IsErrorDescription()
        {
            return CurrentNodeName == "text" && CurrentNodeNamespace == RootElement.ErrorInfoNamespace;
        }
    }
}
